﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProjBackEnd.Common.Helper
{
    public sealed class DBUtility
    {
        /// <summary>
        /// 设置DataSet默认值
        /// </summary>
        /// <param name="ds">DataSet</param>
        /// <param name="strUserid">用户名</param>
        public static void SetDefultValue(DataSet ds, string strUserid)
        {
            DateTime currentDt = DateTime.Now;
            foreach (DataTable tbl in ds.Tables)
            {
                foreach (DataColumn col in tbl.Columns)
                {
                    if (col.ColumnName.ToUpper() == "CREATEDT" && col.DataType == typeof(DateTime))
                    {
                        foreach (DataRow row in tbl.Rows)
                        {
                            if (row.RowState != DataRowState.Added)
                                continue;
                            if (row[col] == null || row[col] == DBNull.Value)
                            {
                                row[col] = currentDt;
                            }
                        }
                        continue;
                    }
                    if (col.ColumnName.ToUpper() == "CREATEID")
                    {
                        foreach (DataRow row in tbl.Rows)
                        {
                            if (row.RowState != DataRowState.Added)
                                continue;
                            row[col] = strUserid;
                        }
                        continue;
                    }
                    if (col.ColumnName.ToUpper() == "UPDATEDT" && col.DataType == typeof(DateTime))
                    {
                        foreach (DataRow row in tbl.Rows)
                        {
                            if (row.RowState != DataRowState.Modified && row.RowState != DataRowState.Added)
                                continue;
                            row[col] = currentDt;
                        }
                        continue;
                    }
                    if (col.ColumnName.ToUpper() == "UPDATEID")
                    {
                        foreach (DataRow row in tbl.Rows)
                        {
                            if (row.RowState != DataRowState.Modified && row.RowState != DataRowState.Added)
                                continue;
                            row[col] = strUserid;
                        }
                        continue;
                    } 
                }
            }
        }
    }
}
